package ext.generic.integration.sourcing.task;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import wt.part.WTPart;

import com.infoengine.SAK.Task;
import com.infoengine.object.factory.Group;
import com.infoengine.util.IEException;
import ext.generic.erp.common.CommonUtil;
import ext.generic.erp.task.IETaskCommonUtil;
import ext.generic.erp.util.IEXMLUtil;
import ext.generic.integration.sourcing.util.PartAttribute;

public class TwoLevelMaterialTask {
	private static final String RELEASE_FLAG = "flag" ;
	private static final String RELEASE_DATE = "releaseDate" ;
	private static List<String> processList = new ArrayList<String>();
	private static IEXMLUtil ieXMLUtil = new IEXMLUtil();
	
	//PDM发布数据成功
	public static final String PDM_RELEASE_STATUS = "N" ;
	
	//单个二级物料
	private static String ieXMLFilePartQuery = "ext/generic/integration/sourcing/part/PartQuery.xml" ;
	private static String ieXMLFilePartCreate = "ext/generic/integration/sourcing/part/PartCreate.xml" ;
	private static String ieXMLFilePartUpdate = "ext/generic/integration/sourcing/part/PartUpdate.xml" ;
	private static String ieXMLFilePartDelete = "ext/generic/integration/sourcing/part/PartDelete.xml" ;
	
	
	
	static{
		processList.add(RELEASE_FLAG);
		processList.add(RELEASE_DATE);
	}
	
	/**
	 * 添加或者更新中间表数据
	 * @param part
	 */
	public static boolean add(WTPart part, String releaseTime) {
		boolean releaseFlag = false;
		if( hasObject( part ) ){
			update( part , releaseTime );
		}else{
			create( part , releaseTime );
		}
		releaseFlag = hasObject( part );
		if(releaseFlag){
			System.out.println(part.getNumber() + "二级物料插入中间表成功");
		}else{
			System.out.println(part.getNumber() + "二级物料插入中间表失败");
		}
		return releaseFlag;
	}
	
	/**
	 * 查询part是否存在中间表中
	 * @param part
	 * @return
	 */
	public static Group query( WTPart part ){
		
		System.out.println(">>>>> Start query Method ...... ") ;
		
		Group group = null ;
		
		if( part == null ){
			return group ;
		}
		
		//创建任务
		Task task = new Task( ieXMLFilePartQuery );

		addQueryWhere( part , task ) ;
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//获取返回值
		group =  task.getGroup("QueryObjects");
		
		if( group == null ){
			System.out.println(">>>>> Query , Group == null") ;
		}else{
			System.out.println(">>>>> Query , Group Size = " + group.getElementCount() ) ;
		}
		
		System.out.println(">>>>> End query Method ...... ") ;
		
		return group ;
	}
	
	/**
	 * 根据指定条件删除中间表数据
	 * @param part
	 */
	public static void delete( WTPart part ){
		
		if( part == null ){
			return;
		}
		
		if( !hasObject( part ) ){
			return;	
		}
		
		Task task = new Task( ieXMLFilePartDelete );
		
		addQueryWhere( part , task );
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加数据到中间表
	 * @param part
	 * @param releaseTime
	 */
	private static void create(WTPart part, String releaseTime) {
		
		if( part == null ){
			return;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml( IETaskCommonUtil.TASK_FILE_DIRECOTRY + ieXMLFilePartCreate );
		
		Task task = new Task( ieXMLFilePartCreate );
		
		if( fieldMap != null ){
			Iterator<String> iteratorKey = fieldMap.keySet().iterator();
			
			while( iteratorKey.hasNext() ){
				String key = iteratorKey.next();
				
				if( key != null && ( !key.equals("") ) ){
					String attributeId = fieldMap.get( key );				
					//中间表添加数据为每一列赋值				
					if( processList.contains( attributeId ) ){
						addProcessAttribute( attributeId , task , releaseTime);
					}else{
						String attributeValue = PartAttribute.getAttributeValue(part, attributeId);
						System.out.println( "Debug   "+attributeId+":"+attributeValue );
						task.addParam( attributeId , attributeValue );
					}		
				}
			}
		}
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	

	/**
	 * 更新数据到中间表
	 * @param part
	 * @param releaseTime
	 */
	private static void update(WTPart part, String releaseTime) {
		
		if( part == null ){
			return ;
		}
		
		//从IE Task的XML文件中解析属性映射关系
		Map<String,String> fieldMap = ieXMLUtil.parserXml(IETaskCommonUtil.TASK_FILE_DIRECOTRY + ieXMLFilePartUpdate );
		
		Task task = new Task( ieXMLFilePartUpdate );
		
		if( fieldMap != null ){
			Iterator<String> iteratorKey = fieldMap.keySet().iterator();
			
			while( iteratorKey.hasNext() ){
				String key = iteratorKey.next();
				
				if( key != null && ( !key.equals("") ) ){
					String attributeId = fieldMap.get( key );				
					//中间表添加数据为每一列赋值				
					if( processList.contains( attributeId ) ){
						addProcessAttribute( attributeId , task , releaseTime);
					}else{
						String attributeValue = PartAttribute.getAttributeValue(part, attributeId);
						task.addParam( attributeId , attributeValue );
					}		
				}
			}
		}
		
		addQueryWhere( part , task );
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 单独添加列的属性
	 * @param attributeId
	 * @param part
	 * @param task
	 * @param releaseTim
	 */
	private static void addProcessAttribute( String attributeId , Task task , String releaseTim ) {
		
		if( releaseTim == null || ( releaseTim.trim() ).equals("") ){
			releaseTim = CommonUtil.getCurrentTime() ;
		}
		
		if( attributeId.equals( RELEASE_FLAG ) ){
			task.addParam( RELEASE_FLAG, PDM_RELEASE_STATUS );
		}else if( attributeId.equals( RELEASE_DATE ) ){
			task.addParam( RELEASE_DATE , releaseTim );
		}
		
	}
	
	/**
	 * 创建查询条件:二级料编号，视图，大版本相同的情况下零部件对象相同
	 * @param part
	 * @param task
	 */
	private static void addQueryWhere( WTPart part , Task task ) {
		
		if( part == null || task == null ){
			return ;
		}
		
		String where = "item_id ='" + PartAttribute.getAttributeValue( part , "secondaryMaterialNumber") + "' " ;
		where = where + " And " + "pview='" + PartAttribute.getAttributeValue(part, "view") + "' " ;
		where = where + " And " + "MAJOR_VERSION='" + PartAttribute.getAttributeValue(part, "majorVersion") + "' " ;
		
		task.addParam( "WHERE",where );
		
	}
	
	/**
	 * 判断数据库中是否存在该数据
	 * @param part
	 * @return boolean
	 * @condition ITEM_ID,PVIEW,MAJOR_VERSSION
	 */
	public static boolean hasObject( WTPart part ){
		System.out.println(">>>>> Start hasObject Method ...... ") ;
		
		boolean hasObject = false ;
		
		Group group = query( part ) ;
		
		if( group != null && group.getElementCount() > 0 ){
			hasObject = true ;
		}
		
		System.out.println(">>>>> hasObject Method , hasObject = " + hasObject ) ;
		
		System.out.println(">>>>> End hasObject Method ...... ") ;
		
		return hasObject ;
	}
	
	/**
	 * 根据零部件编号获取中间表数据
	 * @param part
	 * @return
	 */
	public static Group numberQuery( WTPart part ){
		
		System.out.println(">>>>> Start query Method ...... ") ;
		
		Group group = null ;
		
		if( part == null ){
			return group ;
		}
		
		//创建任务
		Task task = new Task( ieXMLFilePartQuery );

		addNumberQueryWhere( part , task ) ;
		
		try {
			task.invoke();
		} catch (IEException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//获取返回值
		group =  task.getGroup("QueryObjects");
		
		return group ;
	}
	
	/**
	 * 创建查询条件:编号，视图，大版本相同的情况下零部件对象相同
	 * @param part
	 * @param task
	 */
	private static void addNumberQueryWhere( WTPart part , Task task ) {
		
		if( part == null || task == null ){
			return ;
		}
		
		String where = "item_id ='" + PartAttribute.getAttributeValue( part , "secondaryMaterialNumber") + "' " ;
		
		task.addParam( "WHERE",where );
		
	}
	
}
